using System;
using System.Collections.Generic;
using System.Text;
using System.Reflection;
using System.IO;

namespace LizardTFx64Registry
{
	class Program
	{
		static void Main(string[] args)
		{
			string command = args[0];
			BindingFlags bf = BindingFlags.Static | BindingFlags.NonPublic | BindingFlags.IgnoreCase;
			MethodInfo mi = typeof(Program).GetMethod(command, bf);
			try
			{
				mi.Invoke(null, new object[] { args });
				return;
			}
			catch
			{
				System.Environment.ExitCode = 255;
				return;
			}


		}


		private static void RemoveOldOverlays(string[] args)
		{
			bool found = false;
			string regPath = "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Explorer\\ShellIconOverlayIdentifiers\\";
			string[] keys = new string[] {"_CheckedInWritableOverlay","_LizardAddOverlay","_LizardCheckedOutOverlay",
				"_LizardConflictOverlay","_LizardDeletedOverlay","_LizardDifferentOverlay","_LizardLockedOverlay",
				"_LizardNotCheckedOutOverlay","_LizardOutOfDateOverlay","_LizardReadonlyOverlay", "_UnknownStatusOverLay"};
			foreach (string key in keys)
			{
				string regKey = regPath + key;
				Microsoft.Win32.RegistryKey rkOldEntry = Microsoft.Win32.Registry.LocalMachine.OpenSubKey(regKey);
				if (rkOldEntry != null)
				{
					Microsoft.Win32.Registry.LocalMachine.DeleteSubKey(regKey);
					found = true;
				}
			}

			System.Environment.ExitCode = found ? 0 : 1;
		}

		private static void TestForTortoiseOverlays(string[] args)
		{
			object rkTo = Microsoft.Win32.Registry.GetValue("HKEY_LOCAL_MACHINE\\Software\\TortoiseOverlays", "", "");
			System.Environment.ExitCode = (rkTo == null) ? 1 : 0;
		}

		private static void SetLizardOverlayValues(string[] args)
		{
			string asmFolder = Path.GetDirectoryName(Assembly.GetExecutingAssembly().CodeBase).Substring(6);

			string icons = asmFolder + "\\icons";

			Microsoft.Win32.RegistryKey rkOverlays = Microsoft.Win32.Registry.LocalMachine.OpenSubKey("SOFTWARE\\TortoiseOverlays",true);
			rkOverlays.SetValue("UnversionedIcon", icons + "\\TortoiseOutOfDate.ico");
			rkOverlays.SetValue("IgnoredIcon", icons + "\\TortoiseNoCheckOutDiff.ico");
		}


		private static void TestLizardOverlayValues(string[] args)
		{
			Microsoft.Win32.RegistryKey rkOverlays = Microsoft.Win32.Registry.LocalMachine.OpenSubKey("Software\\TortoiseOverlays");
			string keyValue = rkOverlays.GetValue("UnversionedIcon", string.Empty).ToString();


			if (!keyValue.EndsWith("TortoiseOutOfDate.ico"))
			{
				throw new Exception("No Lizard Overlays found.");
			}
		}
	}
}
